var ajaxObj = {
    createXhr: function () {
        var xhrObj = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
        return xhrObj;
    },
    formData: function (data) {
        var formdata = '';
        for ( var i in data) {
            formdata += i + '=' + data[i] + '&';
        }
        return formdata;
    },
    Get: function (xhr,target,callback,string) {
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    callback(xhr.responseText);
                } else {
                    return;
                }
            }
        }
        if (string) {
            xhr.open("GET",target + "?" + string,true);
        } else {
            xhr.open("GET",target,true);
        }
        xhr.send(null);
    },
    Post: function (xhr,target,callback,string) {
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                callback.call(this, xhr.responseText);
            }
        }
        xhr.open('POST', target, true);
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.send(string);
    }
}
var flag = 0;
function loopSearch () {
  var xhr = ajaxObj.createXhr();
  ajaxObj.Get(xhr, './data', function functionName(data) {
    data = JSON.parse(data);
    if (data.direction) {
      document.querySelector('.direction').innerHTML=data.direction;
    } else {
      document.querySelector('.direction').innerHTML='未获取到风向数据呃';
    }
    if (data.wind_scale) {
      document.querySelector('.power').innerHTML=data.wind_scale;
    } else {
      document.querySelector('.power').innerHTML='未获取到风速数据呃';
    }
    if (data.temperature) {
      document.querySelector('.temperature').innerHTML=data.temperature;
    } else {
      document.querySelector('.temperature').innerHTML='未获取到气温数据呃';
    }
    if (data.humidity) {
      document.querySelector('.humidity').innerHTML=data.humidity;
    } else {
      document.querySelector('.humidity').innerHTML='未获取到湿度数据呃';
    }
    setTimeout(function () {
      loopSearch();
      flag++;
    }, 30*1000)
  });
}
setTimeout(function () {
  loopSearch();
  flag++;
}, 30*1000);
